package com.example.springbatch_itemreader_datasource;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Session;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

@SpringBootApplication
@EnableBatchProcessing
@Slf4j
public class SpringApplication16 {

    public static void main(String[] args) {
        SpringApplication.run(SpringApplication16.class, args);
    }

    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Resource
    private StepBuilderFactory stepBuilderFactory;


    @Resource(name = "myJobExecutionListener")
    private JobExecutionListener jobExecutionListener;

    @Resource
    private DataSource dataSource;

    @Resource(name = "userRowMapper")
    private UserRowMapper userRowMapper;

    @Bean
    public ItemWriter<User> itemWriter() {
        return new ItemWriter<User>() {
            @Override
            public void write(List<? extends User> list) throws Exception {
                list.forEach(System.out::println);
            }
        };
    }

    /**
     * 以json的格式进行读取
     * @return
     */
 @Bean
 public JdbcCursorItemReader<User> jdbcCursorItemReader(){


     return new JdbcCursorItemReaderBuilder<User>()
             .name("jdbcCursorBuilder")
             // 定义数据源
             .dataSource(dataSource)
             // 定义执行的sql
             .sql("select * from user where age > ?")
             // 映射的方式
             .rowMapper(userRowMapper)
             // 拼接参数
             .preparedStatementSetter(new ArgumentPreparedStatementSetter(new Object[]{16}))
             .build();

 }


    @Bean
    public Step step() {
        return stepBuilderFactory
                .get("step")
                .<User, User>chunk(1)
//                .reader(userItemReader())
                .reader(jdbcCursorItemReader())
                .writer(itemWriter())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("sql-faster-json").start(step()).
                incrementer(new RunIdIncrementer())
                .listener(jobExecutionListener)
                .build();
    }



}
